﻿using System;
using System.Collections.Generic;
using System.Text;
using Vara.Utility;
using System.Data;

namespace Vara.Asset
{
    public class AssetEvent_Controller : IAssetEvent_Actions
    {
        public void Insert(IAssetEvent AssetEvent, IAsset Asset)
        {
            using (ISqlEngine Engine = VaraSqlEngineProvider.Get())
            {
                ISqlEngineVariable[] var = {
                                           VaraSqlEngineProvider.GetParam("@AssetID",Asset.ID),
                                           VaraSqlEngineProvider.GetParam("@EventName",AssetEvent.EventType),
                                           VaraSqlEngineProvider.GetParam("@Date",AssetEvent.DateAndTime,System.Data.SqlDbType.DateTime),
                                           VaraSqlEngineProvider.GetParam("@Notes",AssetEvent.Description),
                                           VaraSqlEngineProvider.GetParam("@Username",AssetEvent.Username)
                                           };
                Engine.ExecuteProcedure("Insert_Asset_Event", var);
            }
        }

        public List<IAssetEvent> Return(IAsset Asset)
        {
            List<IAssetEvent> EventsToReturn = new List<IAssetEvent>();
            DataTable DataTableEvents;
            using (ISqlEngine Engine = VaraSqlEngineProvider.Get())
            {
                ISqlEngineVariable[] var = {
                                           VaraSqlEngineProvider.GetParam("@AssetID",Asset.ID)};
                DataTableEvents = Engine.ExecuteProcedure("Return_Asset_Events", var);
            }

            foreach (DataRow R in DataTableEvents.Rows)
            {
                IAssetEvent AssetEvent = new AssetEvent(R["EventType"].ToString(),R["Description"].ToString(),R["Username"].ToString(),Convert.ToDateTime(R["Date"].ToString()));
                EventsToReturn.Add(AssetEvent);
            }

            return EventsToReturn;
        }
    }
}
